import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { saveAs } from 'file-saver';
import { map } from "rxjs/operators";
import { Subject } from 'rxjs';
import { Observable } from 'rxjs';
@Injectable()
export class UnitService {
    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();
    constructor(private http: HttpClient) { }
    /**获取组织机构相关字典 */
    initDictInfo(sysId: number) {
        return this.http.get('/system/api/unitinfo/dict?sysId=' + sysId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取组织机构树结构 */
    getUnitTree(unitId: number, isRoot: boolean, isSupervise: string = "false") {
        return this.http.get('/system/api/unitinfo/listtree?unitId=' + unitId + "&isRoot=" + isRoot + "&isSupervise=" + isSupervise).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 单纯的获取机构层级树 */
    getUnitInfoTree(unitId: number, isRoot: boolean){
        return this.http.get('/system/api/unitinfo/listunittree?unitId=' + unitId + "&isRoot=" + isRoot).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取指定机构详情 */
    getUnitInfoDetail(id: number) {
        return this.http.get('/system/api/unitinfo/load?unitId=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除机构信息 */
    deleteUnit(id: number) {
        return this.http.get('/system/api/unitinfo/delete?unitId=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }
    /**保存或更新 */
    saveUpdateUnitInfo(unitInfo: any) {
        return this.http.post('/system/api/unitinfo/saveupdate', unitInfo).pipe(
            map((res: Response) => res['data'])
        )
    }
    /**根据下拉框查找机构信息 */
    searchUnitByName(name: string) {
        return this.http.get('/system/api/unitinfo/search?name=' + name).pipe(
            map((res: Response) => res['data'])
        )
    }
    /**根据选中部门可选执法主体 */
    loadSeniorSubject(unitId: number) {
        return this.http.get('/system/api/unitinfo/loadseniorsubject?unitId=' + unitId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取下级机构进行重排序 */
    getChildrenUnits(unitId: number) {
        return this.http.get('/system/api/unitinfo/children?unitId=' + unitId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 机构重排序 */
    reorderUnit(units: any) {
        return this.http.post('/system/api/unitinfo/reorder', units).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**下载导入模板 */
    downloadTemplate(fileName) {
        return this.http.get('/system/api/unitinfo/template', { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, fileName)))
    }
    /**文件解析 */
    analysisFile(file: any) {
        return this.http.post('/system/api/unitinfo/analyse', file).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**下载导入模板中的错误数据 */
    downloadErrorData(fileName, units) {
        return this.http.post('/system/api/unitinfo/downloaderror', units, { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, fileName)))
    }
    /**数据导入 */
    importData(units: any, parentUnitId: number) {
        return this.http.post('/system/api/unitinfo/import?unitId=' + parentUnitId, units).pipe(
            map((res: Response) => res['data'])
        )
    }

    /**父子组件通讯 */
    announceMission(data: any) {
        this.routeMsgPushSource.next(data);
    }
}
